<?php
namespace App\Models\User;

use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Msg;
use App\Models\BaseModel;
use App\Models\Invite\Invite;

class UserPoint extends BaseModel
{


    protected static function boot()
    {
        parent::boot();
        UserPoint::updating(function($model){
            $old = UserPoint::where('user_id', $model->user_id)->first();
            if($old->total_point < 101 && $model->total_point >= 101){
                $invite = Invite::where('invitee_id', $model->user_id)->first();
                if(!empty($invite) && $invite->inviter_reward == 0){
                    User::where('user_id', $invite->inviter_id)->increment('balance', 10);
                    (new Balance())->add(
                        Msg::reward_I_want_cash().'_'.Msg::reward_inviter(),
                        0,
                        $invite->inviter_id,
                        ConstVar::$BALANCE_CHANNEL_BALANCE,
                        ConstVar::$BALANCE_CHANNEL_BALANCE,
                        ConstVar::$BALANCE_ORIGIN_REWARD,
                        $invite->invitee_id,
                        10,
                        0,0,0,1,
                        ConstVar::$BALANCE_TYPE_SYSTEM,
                        ConstVar::$BALANCE_TYPE_USER
                    );
                    $invite->inviter_reward = 1;
                    $invite->save();
                }
            }
        });
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'updated_at','created_at'
    ];


    protected $table = 'user_point';
    protected $primaryKey = 'user_id';
    protected $dateFormat = 'U';
}
